{% extends 'home/template.html' %}

{% load i18n %}
{% load bleach_tags %}
{% load app_filters %}

{% block title %} {% translate "Product List" %} {% endblock title %}

{% block stylesheets %}
  {{ block.super }}
{% endblock stylesheets %}

{% block content %}

    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>{% translate "Product List" %}</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="/">{% translate "Home" %}</a></li>
              <li class="breadcrumb-item active">{% translate "Products" %}</li>
            </ol>
          </div>
        </div>
      </div>
    </section>

    <section class="content">

    <div class="container-fluid">
      <div class="card card-outline card-primary">
        <div class="card-header">
          <h3 class="card-title">{{ DB_product_query.count }} {% translate "Product" %}{{ DB_product_query.count|pluralize:"" }}</h3>

          <div class="card-tools">
            <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
              <i class="fas fa-minus"></i>
            </button>
          </div>
        </div>
        <div class="card-body">
          <table id="productlist" class="table table-bordered table-hover">
              <thead>
                  <tr>
                      <th style="width: 35%" class="text-center">
                          {% translate "Product" %}
                      </th>
                      <th style="width: 25%" class="text-center">
                          {% translate "Customer" %}
                      </th>
                      <th style="width: 10%" class="text-center">
                          {% translate "Reports" %}
                      </th>
                      <th style="width: 10%" class="text-center">
                          {% translate "Findings" %}
                      </th>
                      <th style="width: 20%" class="text-center">
                          {% translate "Actions" %}
                      </th>
                  </tr>
              </thead>
              <tbody>

                  {% for product in DB_product_query %}

                    <tr>
                      <td>
                        <a href="/product/view/{{ product.pk }}">{{ product.name }}</a>
                      </td>
                      <td>
                        <span class="pull-right badge bg-blue"></span> <a href="{% url 'customer_view' product.customer.pk %}">{{ product.customer.name }}</a>
                      </td>
                      <td>
                        <span class="pull-right badge bg-blue">{{ product.db_report_set.count }}</span> {% translate "Report" %}{{ product.db_report_set.count|pluralize:"" }}
                      </td>
                      <td>
                        <span class="pull-right badge bg-red">{{ product.db_report_set.all|findings_count }}</span> {% translate "Finding" %}{{ product.db_report_set.all|findings_count|pluralize:"" }}
                      </td>
                      <td class="project-actions text-center">
                          <a class="btn btn-primary btn-sm" href="/product/view/{{ product.pk }}">
                              <i class="fas fa-folder"></i>
                              {% translate "View" %}
                          </a>

                          {% if user.groups.all.0|stringformat:'s' == "administrator" %}

                            <a class="btn btn-info btn-sm" href="/product/edit/{{ product.pk }}">
                                <i class="fas fa-pencil-alt"></i>
                                {% translate "Edit" %}
                            </a>

                            <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target=".mm-modal-{{product.pk}}">
                              <i class="fas fa-trash"></i>
                                {% translate "Delete" %}
                            </button>

                          {% endif %}

                      </td>

                    </tr>

                      <div class="modal modal-danger fade mm-modal-{{product.pk}}" id="modal-danger">
                        <div class="modal-dialog">
                          <div class="modal-content bg-danger">
                            <div class="modal-header">
                              <h4 class="modal-title">{% translate "Are you sure?" %}</h4>
                              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                              </button>
                            </div>
                            <div class="modal-body">
                              <p>{% translate "Delete product" %}&hellip;</p>
                            </div>
                            <div class="modal-footer justify-content-between">
                              <button type="button" class="btn btn-outline-light" data-dismiss="modal">{% translate "Close" %}</button>
                              <button type="button" onclick="DeleteID({{product.pk}});" class="btn btn-outline-light">{% translate "Delete product" %}</button>

                            </div>
                          </div>
                        </div>
                      </div>
                     
                  {% endfor %}


              </tbody>
          </table>
        </div>
       
      </div>
      
    </div>
  </section>



{% endblock content %}

{% block javascripts %}
  {{ block.super }}

<script>
  $(function () {
    $("#productlist").DataTable({
      "paging": true,
      "searching": true,
      "responsive": true, 
      "lengthChange": false, 
      "autoWidth": false,
      "buttons": ["copy", "csv", "excel", "pdf", "print", "colvis"]
    }).buttons().container().appendTo('#productlist_wrapper .col-md-6:eq(0)');
  });
</script>


<script>
  function DeleteID(del_id) {
        $.ajax({
            type: 'POST',
            url: "/en/product/delete/",
            headers:{
              "X-CSRFToken": '{{ csrf_token }}'
            },
            data : { 'delete_id': del_id},

            success : function(json) {
              document.location.href = "/product/list/"; 
            }
        })
  }
</script>

{% endblock javascripts %}
